// import Stream from 'node-rtsp-stream'
// const stream = new Stream({
//   name: 'name',
//   streamUrl: 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov',
//   wsPort: 9999,
//   ffmpegOptions: { // options ffmpeg flags
//     '-stats': '', // an option with no neccessary value uses a blank string
//     '-r': 30 // options with required values specify the value after the key
//   }
// })
// ffmpeg -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov -y -f mjpeg -t 0.001 -s 1280x720 test.jpg
// ffmpeg -i rtsp://172.16.10.123:8557/h264 -y -f mjpeg -t 0.001 test.jpg
// ffmpeg -i http://gb28181.wechain360.com/live/34020000001310000404.m3u8?auth_key=1630253621-0-0-9009a2ecd9caadd275b677d515afe24b -y -f mjpeg -t 0.001 test.jpg
import log from "./logger";
import {ffmpeg} from "../node-test";

function get_camera_snapshot(rtsp: string): Promise<Buffer> {
    return new Promise((resolve, reject) => {
        try {
            let chunks: any[] = [];
            let size = 0;
            let outOption = "-vf [in]drawtext=x=10:y=300:fontfile=C\\\\:/Windows/fonts/simhei.ttf:fontsize=60:fontcolor=orange:shadowy=2:text=京A11111(20吨)," +
                "drawtext=x=10:y=400:fontfile=C\\\\:/Windows/fonts/simhei.ttf:fontsize=60:fontcolor=orange:shadowy=2:text=2024年01月25日16时30分[text];" +
                "movie=logo.png[wm];[text][wm]overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)[out]";
            let proc = ffmpeg(rtsp)
                .inputOptions([
                    "-timeout 2000000",
                    "-f rtsp",
                    "-rtsp_transport tcp",
                    "-an"
                ])
                .outputOptions([
                    outOption,
                    "-vframes 1"
                ])
                .format('mjpeg')
                .on('start', function (commendLine) {
                    console.log(commendLine);
                })
                .on('end', async function () {
                    let data = Buffer.concat(chunks, size);
                    // let base64_img = data.toString('base64');
                    resolve(data);
                })
                .on('error', function (err) {
                    log.error('an error happened on get_camera_snapshot:' + err.message);
                    // reject();
                })
            let ffstream = proc.pipe();
            ffstream.on('data', function (chunk) {
                chunks.push(chunk);
                size += chunk.length;
            });
        } catch (error) {
            console.log('---get camera snapshot failed---', error);
            // reject();
        }
    })
}

async function saveImg() {
    // const base64 = await this.get_camera_snapshot(vlpr.rtsp);
    try {
        console.log('start')
        const file = await get_camera_snapshot("rtsp://192.168.1.100:8557/h264")
        console.log('file', file);
    } catch (e) {
        log.error('e:', e);
    }
    console.log('end')
    // get_camera_snapshot("rtsp://192.168.1.100:8557/h264").then((file: any) => {
    //     const data = new FormData();
    //     const stream = require('stream');
    //     const bufferStream = new stream.PassThrough();
    //     const streams = bufferStream.end(file);
    //     data.append('contractFile', streams, {filename: 'img.jpg'});
    //     axios.post(
    //         'https://platform.ncexc-rm.com/api/query/oss/oss2',
    //         data,
    //         {headers: data.getHeaders()}
    //     ).then(function (response) {
    //         console.log(response.data.data);
    //     })
    //         .catch(function (error) {
    //             console.log(error);
    //         });
    // })
}

saveImg()


